package com.itheima.set;

import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
import java.util.function.Predicate;

/*
Set的使用
增
删
删所有
长度
包含
iterator
增强for
foreach

 */
public class Demo02 {

    public static void main(String[] args) {
        Set<String> set= new TreeSet<>();
        //增
        set.add("蔡徐坤1");
        set.add("蔡徐坤1");
        set.add("鹿晗1");
        set.add("鹿晗2");
        set.add("鹿晗3");
        set.add("蔡徐坤5");
        set.add("蔡徐坤3");
        set.add("蔡徐坤4");
        set.add("蔡徐坤4");
        //method01(set);
        System.out.println(set);
        //根据条件删除
     /*   set.removeIf(new Predicate<String>() {
            @Override
            public boolean test(String s) {
                return  s.startsWith("蔡徐坤");
            }
        });*/
        set.removeIf(x->x.endsWith("3"));
        System.out.println(set);
        //包含
        System.out.println(set.contains("吴亦凡"));
        System.out.println(set.contains("鹿晗1"));

        //iterator
        System.out.println("----------------");
        Iterator<String> iterator = set.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
        System.out.println("----------------");
        //增强for
        for (String s : set) {
            System.out.println(s);
        }
        System.out.println("----------------");
        //foreach
    set.forEach(x-> System.out.println(x));
        System.out.println("----------------");


    }

    public static void method01(Set<String> set) {
        System.out.println(set.isEmpty());
        System.out.println(set);
        //长度
        System.out.println(set.size());
        //删
        boolean cai = set.remove("蔡徐坤1");
        System.out.println(cai);
        System.out.println(set.size());
        //删所有
        set.clear();
        System.out.println(set);
        System.out.println(set.size());
        System.out.println(set.isEmpty());
    }
}
